package com.dbzf.dao;

import com.dbzf.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    public static Connection connection;
    public static PreparedStatement stmt;
    public static ResultSet resultSet;
    public static int excute(String sql,Object[] pram) {
        connection = JdbcUtil.getConn();
        int result = 0;
        try {
            stmt = connection.prepareStatement(sql);
            if(pram != null){
                for (int i = 0; i < pram.length; i++) {
                    stmt.setObject(i+1,pram[i]);
                }
            }
            result = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static ResultSet getResultSet(String sql,Object[] pram) {
        connection = JdbcUtil.getConn();
        try {
            stmt = connection.prepareStatement(sql);
            if(pram != null){
                for (int i = 0; i < pram.length; i++) {
                    stmt.setObject(i+1,pram[i]);
                }
            }

            resultSet = stmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    public static void closeAll(Connection connection,PreparedStatement stmt,ResultSet resultSet) {
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(stmt != null){
                stmt.close();
            }
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
